using DaoHang.Models;

namespace DaoHang.Services
{
    /// <summary>
    /// 轻量级日志服务接口
    /// 支持控制台和SQLite数据库双重输出，多线程安全
    /// </summary>
    public interface ILogService
    {
        /// <summary>
        /// 记录信息日志
        /// </summary>
        /// <param name="message">日志消息</param>
        /// <param name="category">日志分类</param>
        /// <param name="userId">用户ID</param>
        /// <param name="additionalData">附加数据</param>
        Task LogInformationAsync(string message, string category = "System", string? userId = null, string? additionalData = null);

        /// <summary>
        /// 记录警告日志
        /// </summary>
        /// <param name="message">日志消息</param>
        /// <param name="category">日志分类</param>
        /// <param name="userId">用户ID</param>
        /// <param name="additionalData">附加数据</param>
        Task LogWarningAsync(string message, string category = "System", string? userId = null, string? additionalData = null);

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message">日志消息</param>
        /// <param name="exception">异常信息</param>
        /// <param name="category">日志分类</param>
        /// <param name="userId">用户ID</param>
        /// <param name="additionalData">附加数据</param>
        Task LogErrorAsync(string message, Exception? exception = null, string category = "System", string? userId = null, string? additionalData = null);

        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="message">日志消息</param>
        /// <param name="category">日志分类</param>
        /// <param name="userId">用户ID</param>
        /// <param name="additionalData">附加数据</param>
        Task LogDebugAsync(string message, string category = "System", string? userId = null, string? additionalData = null);

        /// <summary>
        /// 记录跟踪日志
        /// </summary>
        /// <param name="message">日志消息</param>
        /// <param name="category">日志分类</param>
        /// <param name="userId">用户ID</param>
        /// <param name="additionalData">附加数据</param>
        Task LogTraceAsync(string message, string category = "System", string? userId = null, string? additionalData = null);

        /// <summary>
        /// 获取所有日志分类
        /// </summary>
        /// <returns>日志分类列表</returns>
        Task<List<string>> GetLogCategoriesAsync();

        /// <summary>
        /// 获取日志（带过滤和分页）
        /// </summary>
        /// <param name="level">日志级别</param>
        /// <param name="category">日志分类</param>
        /// <param name="searchText">搜索文本</param>
        /// <param name="page">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns>日志列表和总数</returns>
        Task<(List<LogEntry> Logs, int TotalCount)> GetLogsAsync(string? level = null, string? category = null, string? searchText = null, int page = 1, int pageSize = 20);

        /// <summary>
        /// 清空所有日志
        /// </summary>
        /// <returns>清空的日志数量</returns>
        Task<int> ClearLogsAsync();

        /// <summary>
        /// 分页获取日志
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>日志列表和总数</returns>
        Task<(List<SystemLog> logs, int totalCount)> GetLogsAsync(int pageIndex = 1, int pageSize = 50);

        /// <summary>
        /// 按级别获取日志
        /// </summary>
        /// <param name="level">日志级别</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>日志列表和总数</returns>
        Task<(List<SystemLog> logs, int totalCount)> GetLogsByLevelAsync(string level, int pageIndex = 1, int pageSize = 50);

        /// <summary>
        /// 按时间范围获取日志
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>日志列表和总数</returns>
        Task<(List<SystemLog> logs, int totalCount)> GetLogsByTimeRangeAsync(DateTime startTime, DateTime endTime, int pageIndex = 1, int pageSize = 50);

        /// <summary>
        /// 搜索日志
        /// </summary>
        /// <param name="keyword">搜索关键字</param>
        /// <param name="level">日志级别过滤</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>日志列表和总数</returns>
        Task<(List<SystemLog> logs, int totalCount)> SearchLogsAsync(string keyword, string? level = null, int pageIndex = 1, int pageSize = 50);

        /// <summary>
        /// 获取日志统计信息
        /// </summary>
        /// <returns>各级别日志数量统计</returns>
        Task<Dictionary<string, int>> GetLogStatisticsAsync();

        /// <summary>
        /// 获取所有日志
        /// </summary>
        /// <returns>所有日志列表</returns>
        Task<List<SystemLog>> GetAllAsync();

        /// <summary>
        /// 清理旧日志
        /// </summary>
        /// <param name="daysToKeep">保留天数</param>
        /// <returns>清理的日志数量</returns>
        Task<int> CleanupOldLogsAsync(int daysToKeep = 30);
    }

    /// <summary>
    /// 日志文件信息
    /// </summary>
    public class LogFileInfo
    {
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; set; } = string.Empty;

        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath { get; set; } = string.Empty;

        /// <summary>
        /// 文件大小（字节）
        /// </summary>
        public long FileSize { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>
        public DateTime LastWriteTime { get; set; }

        /// <summary>
        /// 日志类型
        /// </summary>
        public string LogType { get; set; } = string.Empty;
    }
} 